package ljl.alg.misc;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.stream.Collectors;

/**
 * https://leetcode.cn/problems/largest-number/
 * */
public class LargestNumber {

    public String largestNumber(int[] nums) {
        LinkedList<Integer> list = Arrays.stream(nums).boxed().sorted((a, b) ->
                        -String.valueOf(a).concat(String.valueOf(b))
                                .compareTo(String.valueOf(b).concat(String.valueOf(a))))
                .collect(Collectors.toCollection(LinkedList::new));
        while (!list.isEmpty() && list.peekFirst() == 0)
            list.pollFirst();
        if (list.isEmpty())
            return "0";
        StringBuilder res = new StringBuilder();
        list.forEach(res::append);
        return res.toString();
    }
}
